home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / inventorTemplates / walk.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.0 KB  |  245 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * Copyright (C) 1992   Silicon Graphics, Inc.
  19.  *
  20.  _______________________________________________________________________
  21.  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
  22.  |
  23.  |   $Revision: 1.0000 $
  24.  |
  25.  |   File: walk.c++
  26.  |   Purpose: creates a walk viewer, when the user comes out of viewing
  27.  |            mode and hits the d key, information about the camera
  28.  |            location gets spit out, this is useful for creating
  29.  |            animation files to be played with the player program
  30.  |
  31.  |   Author(s)          : Kevin Goldsmith
  32.  |
  33.  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
  34.  _______________________________________________________________________
  35.  */
  36.  
  37.  
  38.  
  39.  
  40. #include <assert.h>
  41. #include <getopt.h>
  42.  
  43. #include <Inventor/Xt/SoXt.h>
  44. #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
  45. #include <Inventor/nodes/SoNode.h>
  46. #include <Inventor/nodes/SoSeparator.h>
  47. #include <Inventor/nodes/SoPerspectiveCamera.h>
  48. #include <Inventor/SoDB.h>
  49. #include <Inventor/nodes/SoCallback.h>
  50. #include <Inventor/actions/SoHandleEventAction.h>
  51. #include <Inventor/events/SoKeyboardEvent.h>
  52.  
  53. SoCamera *camera = NULL;
  54.  
  55. static char *filename = NULL;
  56. static char *envfile = NULL;
  57.  
  58. void
  59. CreateCamera(SoGroup *root)
  60. {
  61.     SoPerspectiveCamera *pc = new SoPerspectiveCamera;
  62.       pc->ref();
  63.     camera = pc;
  64.     root->insertChild(pc, 0);
  65. }
  66.  
  67. //
  68. // Read a file given a filename and return a separator containing all
  69. // of the stuff in the file.
  70. // --- stolen mostly from Gavin Bell
  71. //
  72. SoSeparator *
  73. readFile(const char *filename)
  74. {
  75.     SoInput in;
  76.     if (filename != NULL)
  77.     {
  78.     if (in.openFile(filename) == FALSE)
  79.     {
  80.         fprintf(stderr, "Could not open file %s\n", filename);
  81.         return NULL;
  82.     }
  83.     }
  84.  
  85.     SoSeparator *graph = new SoSeparator;
  86.     graph->ref();
  87.  
  88.     //
  89.     // Keep on reading until there are no more nodes to read
  90.     //
  91.     SoNode *root;
  92.     do
  93.     {
  94.     int read_ok = SoDB::read(&in, root);
  95.  
  96.     if (!read_ok)
  97.     {
  98.         fprintf(stderr, "Error reading file\n");
  99.         graph->unref();
  100.         return NULL;
  101.     }
  102.     else if (root != NULL) graph->addChild(root);
  103.  
  104.     } while (root != NULL);
  105.     in.closeFile();
  106.  
  107.     //
  108.     // Try to avoid creating extra separators; if this scene graph
  109.     // already has exactly one separator at the top, use it.  This
  110.     // will avoid an explosion of separators at the top of scenes that
  111.     // would otherwise occur if we automatically created a new
  112.     // separator every time a scene graph was read.
  113.     //
  114.     if (graph->getNumChildren() == 1 &&
  115.     graph->getChild(0)->isOfType(SoSeparator::getClassTypeId()))
  116.     {
  117.     SoSeparator *result = (SoSeparator *)graph->getChild(0);
  118.     result->ref();    // Note the order here!
  119.     graph->unref();
  120.  
  121.     result->unrefNoDelete();
  122.     return result;
  123.     }
  124.  
  125.     graph->unrefNoDelete();
  126.     return graph;
  127.  
  128. }
  129.  
  130. // did the user press the key we want?
  131. static void checkKey(void *, SoAction *action)
  132. {
  133.     if (action->getTypeId().
  134.      isDerivedFrom(SoHandleEventAction::getClassTypeId())) {
  135.     const SoEvent *e = ((SoHandleEventAction *)action)->getEvent();
  136.  
  137.     // did the user hit the d?
  138.     if (SO_KEY_PRESS_EVENT(e, D)) {
  139.         assert(camera != NULL);
  140.         SbVec3f pos;
  141.         pos = camera->position.getValue();
  142.  
  143.         SbVec3f axis;
  144.         float angle;
  145.  
  146.         camera->orientation.getValue(axis, angle);
  147.         printf("%f %f %f %f %f %f %f\n", pos[0],
  148.            pos[1], pos[2], axis[0], axis[1], axis[2], angle);
  149.     }
  150.     }
  151. }
  152.  
  153.  
  154. // get user flags
  155. static void
  156. parseCommandLine(int argc, char **argv)
  157. {
  158.     int err = 0;    // Flag: error in options?
  159.     int c;
  160.     
  161.     while ((c = getopt(argc, argv, "e:")) != -1)
  162.     {
  163.     switch(c)
  164.     {
  165.       case 'e': envfile = optarg; break;
  166.       default:
  167.         err = 1;
  168.         break;
  169.     }
  170.     }
  171.     if (optind+1 != argc) err = 1;
  172.  
  173.     filename = argv[optind];
  174.  
  175.     if (err)
  176.     {
  177.     fprintf(stderr, "Usage: %s "
  178.               "[-e envfile] filename\n", argv[0]);
  179.     fprintf(stderr, "-e : Reads given environment file\n");
  180.     exit(99);
  181.     }
  182. }
  183.  
  184.  
  185.  
  186. main( int argc, char **argv )
  187. {
  188.     parseCommandLine(argc, argv);
  189.  
  190.     Widget top = SoXt::init(argv[0]);
  191.     if (top == NULL)
  192.     {
  193.     fprintf(stderr, "%s: Couldn't open top-level widget\n", argv[0]);
  194.     exit(1);
  195.     }
  196.  
  197.     // create the viewer
  198.     SoXtWalkViewer *ra = new SoXtWalkViewer(top);
  199.  
  200.     // create the root of the subgraph
  201.     SoSeparator *root = new SoSeparator;
  202.     root->ref();
  203.  
  204.     // create a callback to see if we get the key
  205.     SoCallback *callback = new SoCallback;
  206.     callback->setCallback(checkKey);
  207.     root->addChild(callback);
  208.  
  209.  
  210.     // the user wants to start at an envfile
  211.     if (envfile != NULL)
  212.     {
  213.     SoSeparator *env = readFile(envfile);
  214.     if (env == NULL) exit(1);
  215.     env->ref();
  216.     for (int i = 0; i < env->getNumChildren(); i++)
  217.     {
  218.         root->addChild(env->getChild(i));
  219.         if (env->getChild(i)->isOfType(SoCamera::getClassTypeId()))
  220.         camera = (SoCamera *)env->getChild(i);
  221.     }
  222.     env->unref();
  223.     } else 
  224.       CreateCamera(root); // otherwise we create our own camera
  225.  
  226.     // read in the inventor file
  227.     SoSeparator *stuff = readFile(filename);
  228.     if (stuff == NULL) exit(1); // it was empty!!
  229.     root->addChild(stuff);      // or it was not
  230.  
  231.     // set some stuff on the render area
  232.     ra->setSceneGraph(root);
  233.     ra->setHeadlight(TRUE);
  234.     ra->setDecoration(TRUE);
  235.  
  236.     //show the viewer
  237.     ra->show();
  238.  
  239.     // show the window
  240.     SoXt::show(top);
  241.  
  242.     // let Inventor take care of the rest
  243.     SoXt::mainLoop();
  244. }
  245.